package src.main.java.org.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/*
* FileInputStream
* */
public class Test1 {
    public static void main(String[] args) throws Exception {
        //1.创建文件字节输入流管道，与源文件接通
///     InputStream fileInputStream = new FileInputStream(new File("E:\\JavaLearn\\javaCode\\src\\test1.txt"));
        InputStream inputStream = new FileInputStream("E:\\JavaLearn\\javaCode\\src\\test1.txt");

        //2.开始读取文件字节数据
/*        int read1 = inputStream.read();
        System.out.println((char) read1);

        int read2 = inputStream.read();
        System.out.println((char) read2);

        int read3 = inputStream.read();
        System.out.println(read3);//没有数据后返回-1*/

        //3.使用循环读取文件
        int b;//用于记住读取的字节
        /*while ((b = inputStream.read())!=-1){
            System.out.print((char) b);
        }*/

        //读取数据的性能很差
        //读取汉字输出乱码，无法避免
        //流使用完毕后必须关闭，释放系统资源
        inputStream.close();

        //文件字节输入流，每次读取多个字节
        InputStream is = new FileInputStream("E:\\JavaLearn\\javaCode\\src\\test2.txt");
        //开始读取文件字节数据，每次读取多个字节
  /*      byte[] buffer = new byte[3];
        int len = is.read(buffer); //返回读取的字节数量，读取完毕返回-1
        String s = new String(buffer,0,len);
        System.out.println(s);
        System.out.println(len); //单次读取的字节数量

        int len1 = is.read(buffer);
        String s1 = new String(buffer,0,len1);
        System.out.println(s1);
        System.out.println(len1); //单次读取的字节数量*/

//        优化
        byte[] buffer = new byte[3];
        int len;
        while((len = is.read(buffer))!=-1){
            String s = new String(buffer, 0, len);
            System.out.println(s);
        }
        is.close();
        /*
        * 优点：性能得到提升，调用次数减少
        * 缺点：
        *   1.读取汉字也有乱码
        * */

    }
}
